<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>表格Demo</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css" media="all">
</head>

<body>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>添加运动</legend>
    </fieldset>

    <form class="layui-form" action="" lay-filter="example" method="POST" action="" style="padding: 10px 50px;;">
        <div class="layui-form-item">
            <label class="layui-form-label">运动姓名</label>
            <div class="layui-input-block">
                <input type="text" name="motionType" lay-verify="required" autocomplete="off" placeholder=""
                    id="motionType" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">运动热量</label>
            <div class="layui-input-block">
                <input type="text" name="motionHot" lay-verify="required" autocomplete="off" placeholder=""
                    id="motionHot" class="layui-input" type="number">
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">运动图片:</label>
            <div class="layui-input-block" style="margin-right: 20px">
                <div class="layui-upload-list" id="motionBtnDel"
                    style="height: 100px; width: 100px;border: 1px solid #cccccc;">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="submit" lay-submit="" lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script src="../../assets/libs/layui/layui.js" charset="utf-8"></script>

    <script>

        layui.config({
            base: '/module/'
        }).use(['jquery', 'layer', 'table', 'form', 'config', 'laydate', 'upload'], function () {

            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;
            var config = layui.config;
            var laydate = layui.laydate;
            var upload = layui.upload;
            var files = null;


            upload.render({
                elem: '#motionBtnDel'
                , auto: false
                , choose: (obj) => {
                    obj.preview((index, file, result) => {
                        let imgUrl = result;
                        files = file;
                        $('#motionBtnDel').html('<div class="imgBox " style="text-align:center;"><img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" width="100" height="100"><a class="remove_' + index + ' imgitem  layui-btn layui-btn-xs layui-btn-danger demo-delete">取消</a></div>')
                        $('.remove_' + index).bind('click', function (e) {
                            e.stopPropagation()
                            files = null;
                            $(this).parent().remove();
                        })
                    });
                }
            });

            $('#submit').on('click', function () {
                var data = form.val('example');
                var motionHot = data.motionHot;
                var motionType = data.motionType;
                var formData = new FormData();
                formData.append('motionType', motionType);
                formData.append('motionHot', motionHot);
                if (files) {
                    formData.append('motionImg', files)
                } else {
                    layer.msg('运动项目图片不能为空', { icon: 5 });
                    return false;
                }
                $.ajax({
                    type: "POST",
                    url: config.base_server + "addMotion",
                    async: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        if (data.code == 1) {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            console.log("可以关闭嘛？");
                            alert("添加成功");
                            parent.layer.close(index); //再执行关闭   
                        } else {
                            alert(data.msg);
                        }
                    },
                    error() {
                        alert("文件太大")
                    }
                });
                return false;
            });

        });
    </script>


</body>